#!/usr/bin/python
# -*- coding: utf-8 -*-

import gtk, gobject, os
import threading, thread
import urllib, urllib2, time
import socket
socket.setdefaulttimeout(15)

class Main:
	def __init__(self):
		self.pluginName = 'MusicDumper'
		self.author = 'Ovidiu D. Niţan'
		self.version = '0.1'
		self.descriere = 'Plugin for searching music on MusicDumper'

	def start(self, pymaxe):
		try:
			pymaxe.musicdumper
		except:
			pymaxe.musicdumper = gtk.RadioMenuItem(pymaxe.gui.get_object('radiomenuitem1'), 'MusicDumper')
			pymaxe.musicdumper.show()
			pymaxe.gui.get_object('menu2').prepend(pymaxe.musicdumper)
			pymaxe.sites[self.pluginName] = 'musicdumper.com'
			
	def search(self, pymaxe, string):
		print "[plugin] musicdumper.com: searching for " + string
		icon = gtk.gdk.pixbuf_new_from_file(__path__[0] + '/musicdumper.png')
		req = urllib2.Request('http://www.musicdumper.com/?search=' + urllib.quote(string));
		getdata = urllib2.urlopen(req);
		data = getdata.read();
		results = data.split('<div class="list_result">')
		results.pop(0)
		for x in results:
			try:
				gtitle = x.split('class="link">')
				gtitle = gtitle[1].split('</a><br />')
				title = urllib.unquote(gtitle[0])
				gurl = x.split('" class="link">')
				gurl = gurl[0].split('<a href="')
				url = 'http://www.musicdumper.com/' + gurl[1]
				gobject.idle_add(pymaxe.addToList, [icon, '<b>' + title.replace('&', '&amp;') + '</b>', '', url])
			except:
				pass
	
	def getDetails(self, pymaxe, url):
		try:
			data = self.fileData(url)
			if data == False:
				pymaxe.isWorking = False
				gobject.idle_add(self.showDialog, 'Error', 'Couldn\'t parse informations about this file. Please check if you have a working network connection and if you use the latest version of plugin.')
				gobject.idle_add(pymaxe.restoreState)
				return
			gobject.idle_add(pymaxe.showDetails, data)
		except Exception, e:
			pymaxe.isWorking = False
			gobject.idle_add(self.showDialog, 'Error', 'Couldn\'t parse informations about this file. Please check if you have a working network connection and if you use the latest version of plugin.\n\n\nDebug info:\n' + str(e))
			gobject.idle_add(pymaxe.restoreState)
			return False		
		#pymaxe.showDetails(url, titlu, durata, downurl, tip)

	def fileData(self, url):
		url = url.replace('&amp;', '&')
		url = url.replace(' ', '+')
		url = urllib.unquote(url[7:])
		url = 'http://' + url
		req = urllib2.Request(url);
		getdata = urllib2.urlopen(req);
		data = getdata.read();
		try:	
			gdownurl = data.split('player.swf?file=')
			gdownurl = gdownurl[1].split('&image=')
			downurl = urllib.unquote(gdownurl[0])
			gtitle = data.split('<h1 class="song-title">')
			gtitle = gtitle[1].split('</h1>')
			titlu = gtitle[0]
			durata = 'N/A'
			
			data = {"url" : url,
				"titlu" : titlu,
				"durata" : durata,
				"tip" : '.mp3',
				"format" : 'audio',
				"downurl" : downurl}
			return data
		except Exception, e:
			print e
			return False

	def showDialog(self, title, message):
			dialog = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_CLOSE, message_format=message);
			dialog.set_title(title);
			dialog.run();
			dialog.destroy();
	
	def uninstall(self, pymaxe):
		pymaxe.musicdumper.destroy()
		del pymaxe.musicdumper
